package com.stockz.equity.charting.painter.indicator;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.Displayable;
import com.stockz.equity.charting.util.Constant;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.util.MathUtil;
import com.stockz.equity.charting.view.StockChartPanelSkin;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.StockRectangle;
import com.stockz.equity.pattern.Painter;

@Component
public class IndicatorValuePainter implements Painter<Displayable> {

	@Override
	public void paint(Graphics2D g, Displayable indicatorDisplay, Rectangle r) {
		if (r != null && !r.isEmpty()) {
			
			StockChartView stockChartView = (StockChartView)ContextUtil.getApplicationContext().getBean(StockChartView.class);
			StockChartPanelState state = stockChartView.getSelectedStockChart()
					.getStockChartPanel().getStockChartPanelState();
			StockChartPanelSkin skin = stockChartView.getSelectedStockChart()
			.getStockChartPanel().getStockChartPanelSkin();
			
			StockRectangle rectangle = (StockRectangle)r;
			double min = rectangle.getMinimumValueVisible();
			double max = rectangle.getMaximumValueVisible();
			g.setClip(null);
			g.setColor(skin.getAxisVolumeColor());
			int divisions = 4;
			double increment = (rectangle.getMaximumValueVisible() - rectangle.getMinimumValueVisible())/divisions;
			if(increment == 0){
				increment = rectangle.getMaximumValueVisible();
			}
			if(increment == 0){
				increment = 1;
			}
			int height = (int)g.getFont().getStringBounds("M", g.getFontRenderContext()).getHeight();
			while(min <= max){
				int Y = MathUtil.fitY(
						rectangle.y,
						rectangle.y + rectangle.height,
						rectangle.getMinimumValueVisible(),
						max, 
						min,
						state.isSemiLog());
				g.drawLine(rectangle.x+rectangle.width, Y, rectangle.x+rectangle.width+ skin.getShortTickLength(), Y);
				g.drawString(Constant.priceNumberFormat.format(min), rectangle.x+rectangle.width+3, Y+height+3);
				min += increment;
			}
		}
	}
}
